package com.example.freeengine.wallpaper.fragment.common.picturelist;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.example.freeengine.R;
import com.example.freeengine.wallpaper.alarm.MyAlarmWorker;
import com.example.freeengine.wallpaper.fragment.PictureInfo;
import com.example.freeengine.wallpaper.fragment.WallPaperViewModel;
import com.example.freeengine.wallpaper.service.MyWallPaperService;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.List;

/**
 * @author Terry Deng
 */
public class PictureListAdapter extends RecyclerView.Adapter<PictureListAdapter.MyViewHolder> {

    List<PictureInfo> pictureInfoList;
    Context context;

    WallPaperViewModel wallPaperViewModel;

    public PictureListAdapter(){}
    public PictureListAdapter(List<PictureInfo> pictureInfoList){
        this.pictureInfoList = pictureInfoList;
    }

    public PictureListAdapter(List<PictureInfo> pictureInfoList, Context context){
        this.pictureInfoList = pictureInfoList;
        this.context = context;
    }
    public PictureListAdapter(List<PictureInfo> pictureInfoList, Context context, WallPaperViewModel wallPaperViewModel){
        this.pictureInfoList = pictureInfoList;
        this.context = context;
        this.wallPaperViewModel = wallPaperViewModel;
    }

    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new MyViewHolder(layoutInflater.inflate(R.layout.wallpaper_picture_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {

        //Drawable drawable = Drawable.createFromPath(pictureInfoList.get(position).getPath());
        //holder.ivPicture.getLayoutParams().width = drawable.getIntrinsicWidth();
        holder.ivPicture.getLayoutParams().height = 200;

        holder.ivPicture.setImageURI(Uri.parse(pictureInfoList.get(position).getPath()));
        holder.ivItemMenu.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(context, v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_wallpaper_picture_list_item, popup.getMenu());
            popup.show();

            popup.setOnMenuItemClickListener(item -> {

                switch (item.getItemId()){
                    case R.id.wallpaper_set: {

                        Intent intent = new Intent(context, MyWallPaperService.class);
                        intent.putExtra("path", pictureInfoList.get(position).getPath());
                        context.startService(intent);

                        new MyAlarmWorker.Builder(context).stopAlarm();

                        return true;
                    }
                    case R.id.wallpaper_picture_full: {
                        NavController navController = Navigation.findNavController(v);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("pictureInfo", pictureInfoList.get(position));
                        navController.navigate(R.id.action_pictureListFragment_to_pictureFullFragment, bundle);
                        return true;
                    }
                    case R.id.wallpaper_picture_delete: {
                        File file = new File(pictureInfoList.get(position).getPath());
                        boolean delete = file.delete();
                        if (delete){
                            pictureInfoList.remove(position);
                            wallPaperViewModel.pictureListDataChange.setValue(1);
                            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    }
                    default:
                        return false;
                }
            });

        });


    }

    @Override
    public int getItemCount() {
        return pictureInfoList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivPicture, ivItemMenu;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            ivItemMenu = itemView.findViewById(R.id.iv_item_menu);
        }
    }

}
